package com.lz.football.module.compete.item
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.events.TaskEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.compete.vo.NationTaskVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.task.vo.TaskInfoVo;
	import com.lz.football.module.bag.item.BagItem;
	import com.lz.football.module.base.IComponent;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.BaseView.GoodItem;
	import common.item.GoodsItem;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 *国家任务item 
	 * @author Administrator
	 * 
	 */	
	public class NationTaskItem extends Sprite implements IComponent
	{
		/**
		 * 
		 */
		private var _skin:MovieClip;
		/**
		 *任务图标  
		 */
		private var taskIcon:MovieClip;
//		/**
//		 *任务名称  
//		 */
//		private var taskName:TextField;
		/**
		 *任务描述 
		 */
		private var taskDes:TextField;
		/**
		 *发布任务需要的欧元 
		 */
		private var needCost:TextField;
		/**
		 *已经发布 
		 */
		private var already:TextField;
		/**
		 *发布按钮  
		 */
		private var issusBtn:SimpleButton;
		/**
		 *领取按钮 
		 */
		private var getBtn:SimpleButton;
		
		/**
		 *任务状态 
		 */
		private var taskStatus:TextField;
		/**
		 * 
		 */
		private var _taskVo:NationTaskVo;
		/**
		 *奖励容器 
		 */
		private var awardContainer:MovieClip;
		/**
		 *效果 
		 */
		private var eff:MovieClip;
		
		public function NationTaskItem()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = Reflection.createInstance("com.lz.modul.compete.nationTaskItem");
			this.addChild(_skin);
			needCost = _skin.need.needCost;
			taskIcon = _skin.taskIcon;
//			taskName = _skin.taskName;
			taskDes = _skin.taskDes;
			already = _skin.already;
			issusBtn = _skin.issusBtn;
			taskStatus = _skin.taskStatus
			awardContainer = _skin.wardContain;
			getBtn = _skin.getBtn;
			eff = _skin.eff;
			eff.gotoAndStop(1);
			eff.visible = false;
			addEvtListener();
			already.mouseEnabled = false;
			reset();
			
		}
		
		public function addEvtListener():void
		{
			issusBtn.addEventListener(MouseEvent.CLICK,onIssusHandler);
		}
		/**
		 * 
		 * 
		 */
		public function setData($vo:NationTaskVo):void
		{
			reset();
			_taskVo = $vo;
			setWard();
			taskIcon.gotoAndStop($vo.id - 400);
//			taskName.text = $vo.name;
			taskDes.text = $vo.des;
			if($vo.condition <10)//AppData.getInstance().clubDatamanager.clubInfo.coin)
			{
				issusBtn.visible = true;
				_skin.taskbg.alpha = 0.3;
			}else
			{
				_skin.need.visible = true;
				needCost.text = $vo.condition + "";
			}
		}
		
		/**
		 *设置奖励 
		 * 
		 */
		public function setWard():void
		{
			var xml:XML =  AppData.getInstance().taskManager.getTaskItemById(_taskVo.id)
			if(xml == null)return;
			var vo:TaskInfoVo = new TaskInfoVo();
			vo.analysis(xml);
			var baseItem:MovieClip;
			if(8 > 0)
			{
				baseItem = Reflection.createInstance("com.lz.commonUI.euro");
				baseItem.x = 60 * awardContainer.numChildren;
				baseItem.y = 2;
				awardContainer.addChild(baseItem);
				DisplayMouseEventManager.instance.addTipsDisplay(baseItem,"欧元奖励：" + vo.coin);
			}
			if(vo.exp > 0)
			{
				baseItem = Reflection.createInstance("com.lz.commonUI.exp");
				baseItem.x = 60 * awardContainer.numChildren;
				awardContainer.addChild(baseItem);
				DisplayMouseEventManager.instance.addTipsDisplay(baseItem,"经验奖励：" + vo.exp);
			}
			if(vo.fame > 0)
			{
				baseItem = Reflection.createInstance("com.lz.commonUI.fram");
				baseItem.x = 60 * awardContainer.numChildren;
				awardContainer.addChild(baseItem);
				DisplayMouseEventManager.instance.addTipsDisplay(baseItem,"声望奖励：" + vo.fame);
			}
			var bagItem:GoodItem;
			var goods:GoodsVo;
			goods= AppData.getInstance().goodsManager.getGoodsByTypeAndId(vo.ntype1,vo.pid1);
			if(goods != null)
			{
				bagItem = new GoodItem();
				bagItem.initUI();
				bagItem.data = goods;
//				bagItem.removeEvtListener();
				bagItem.x = 60 * awardContainer.numChildren;
				awardContainer.addChild(bagItem);
			}
			goods = null;
			goods= AppData.getInstance().goodsManager.getGoodsByTypeAndId(vo.ntype2,vo.pid2);
			if(goods != null)
			{
				bagItem = new GoodItem();
				bagItem.initUI();
				bagItem.data = goods;
//				bagItem.removeEvtListener();
				bagItem.x = 60 * awardContainer.numChildren;
				awardContainer.addChild(bagItem);
			}
			goods = null;
			goods= AppData.getInstance().goodsManager.getGoodsByTypeAndId(vo.ntype3,vo.pid3);
			if(goods != null)
			{
				bagItem = new GoodItem();
				bagItem.initUI();
				bagItem.data = goods;
				bagItem.x = 60 * awardContainer.numChildren;
				awardContainer.addChild(bagItem);
			}
		}
		
		/**
		 *从国家界面打开 
		 * 
		 */
		public function setNationStatus():void
		{
			reset();
			if(_taskVo.taskStatus)
			{
				if(_taskVo.condition <= AppData.getInstance().competeDataManager.competeInfoVo.homecoin * 1000)
				{
					issusBtn.visible = true;
					
				}else
				{
					_skin.need.visible = true;
					needCost.text = _taskVo.condition + "";
				}
				_skin.taskbg.alpha = 0.3;
			}else//已经发布
			{
				already.text = "已发布";
			}
		}
		/**
		 *更新从国家界面打开 
		 * 
		 */
		public function updateNationStatus():void
		{
			setNationStatus();
		}
		/**
		 *更新任务状态  
		 * 
		 */
		public function upDateTaskStatus():void
		{
			reset();
			_skin.taskbg.alpha = 0.3;
			var vo:TaskInfoVo = AppData.getInstance().taskManager.getNationTask(_taskVo.id);
			
			if(vo == null)//任务未发布或者已经完成
			{
				if(_taskVo.taskStatus)//未发布
				{
					already.text = "未发布";
					already.textColor = 0xFFFFFF;
				}else//已经发布完成
				{
					already.text = "任务完成";
					_skin.taskbg.alpha = 1;
					
				}
			}else//任务已经发布 完成或者未完成
			{
				if(vo.f_value1 >= vo.value1 && vo.f_value2 >= vo.value2)
				{
					taskStatus.text = "已完成";
					getBtn.visible = true;
					getBtn.addEventListener(MouseEvent.CLICK,onGetHandler);
					eff.play();
					eff.visible = true
				}else
				{
					taskStatus.text = "任务进度";
					already.text = vo.f_value1 + "/" + vo.value1 
				}
			}
		}
		
		/**
		 *任务完成，领取奖励 
		 * @param event
		 * 
		 */
		protected function onGetHandler(event:MouseEvent):void
		{
			AppEventDispatcher.getInstance().dispatchEvent(new TaskEvent(TaskEvent.GET_TASK_FINISH_WARD,{id:_taskVo.id}));
		}
		/**
		 *重置 
		 * 
		 */
		private function reset():void
		{
			_skin.need.visible = false;
			issusBtn.visible = false;
			taskStatus.text = "";
			
			getBtn.visible = false;
			already.text = "";
			_skin.taskbg.alpha = 1;
			eff.gotoAndStop(1);
			eff.visible = false;
		}
		protected function onIssusHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.ISSUETASK,{id:_taskVo.id}));
		}
		
		public function removeEvtListener():void
		{
			issusBtn.removeEventListener(MouseEvent.CLICK,onIssusHandler);
			getBtn.removeEventListener(MouseEvent.CLICK,onGetHandler);
		}
		
		public function destroyView():void
		{
			reset();
			removeEvtListener();
			while(awardContainer.numChildren)
			{
				var m:DisplayObject = awardContainer.removeChildAt(0)
				if(m is MovieClip)
				{
					DisplayMouseEventManager.instance.removeTipsDisplay(m);
				}else if(m is GoodItem)
				{
					(m as GoodItem).destroyView();
				}
				m = null;
			}
				
			if(this.parent)this.parent.removeChild(this);
			_skin = null;
		}
	}
}